package org.esk.dablog.service.spi;

import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.Resource;

import javax.imageio.ImageIO;
import java.io.File;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.awt.image.BufferedImage;
import java.awt.geom.AffineTransform;
import java.awt.geom.RoundRectangle2D;
import java.awt.*;

/**
 * This class used to create thumbnail from the file
 * User: jc
 * Date: 29.11.2006
 * Time: 19:28:45
 * $Id:$
 */
public class ThumbnailsHelper implements ResourceLoaderAware {
    private int thumbnailHeight;
    private int thumbnailWidth;
    private String backgroundLocation;
    private BufferedImage cachedBackground;
    private ResourceLoader resourceLoader;

    /**
     * creates thumbnail from given data
     *
     * @param imageData
     * @return
     */
    public byte[] createThumbnail(byte[] imageData) throws Exception {
        BufferedImage image = ImageIO.read(new ByteArrayInputStream(imageData));

        //114-114 is a center of a Polaroid, thumb width and height hardcoded to fit the border
        double thumbnailWidth = 170;
        double thumbnailHeight = 180;

        double sx = ((double) thumbnailWidth) / ((double) image.getWidth());
        double sy = ((double) thumbnailHeight) / ((double) image.getHeight());
        //preserve constraints
        double delta = Math.min(sx, sy);

        int newWidth = (int) (((double) image.getWidth()) * delta);
        int newHeight = (int) (((double) image.getHeight()) * delta);


        BufferedImage bg = getBackground();

        BufferedImage thumb = new BufferedImage(bg.getWidth(), bg.getHeight(),
                bg.getType());
        Graphics2D g = thumb.createGraphics();

        g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        double moveX = 110 - ((double) newWidth / 2);
        double moveY = 118 - ((double) newHeight / 2);

        AffineTransform af = AffineTransform.getTranslateInstance(moveX, moveY);
        af.concatenate(AffineTransform.getRotateInstance(Math.toRadians(-2.5)));
        af.concatenate(AffineTransform.getScaleInstance(delta, delta));
        g.drawImage(bg, null, null);
        g.drawImage(image, af, null);
        g.dispose();

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ImageIO.write(thumb, "JPEG", out);
        return out.toByteArray();
    }

    /**
     * caches and returns bg image
     *
     * @return
     * @throws IOException
     */
    private BufferedImage getBackground() throws IOException {
        if (cachedBackground == null) {
            Resource res = resourceLoader.getResource(backgroundLocation);
            cachedBackground = ImageIO.read(res.getFile());
        }

        return cachedBackground;
    }


    public void setResourceLoader(ResourceLoader resourceLoader) {
        this.resourceLoader = resourceLoader;
    }

    public int getThumbnailHeight() {
        return thumbnailHeight;
    }

    public void setThumbnailHeight(int thumbnailHeight) {
        this.thumbnailHeight = thumbnailHeight;
    }

    public int getThumbnailWidth() {
        return thumbnailWidth;
    }

    public void setThumbnailWidth(int thumbnailWidth) {
        this.thumbnailWidth = thumbnailWidth;
    }


    public void setBackgroundLocation(String backgroundLocation) {
        this.backgroundLocation = backgroundLocation;
    }
}
